package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mff
 * @date 2022/6/12 23:05
 * @mood happy
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart){
/*        //        当前分量
        BigDecimal dishNub;
        if(shoppingCart.getNumber()==null){
            dishNub = new BigDecimal(1);
        }else{
            dishNub = new BigDecimal(shoppingCart.getNumber());
        }
//        当前单价
        BigDecimal danJia = shoppingCart.getAmount().divide(dishNub);*/

//        A. 获取当前登录用户，为购物车对象赋值
        shoppingCart.setUserId(BaseContext.getCurrentId());
//        B. 根据当前登录用户ID 及 本次添加的菜品ID/套餐ID，查询购物车数据是否存在
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId, shoppingCart.getDishId());
        ShoppingCart shoppingCartRecord = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
//        C. 如果已经存在，就在原来数量基础上加1
        if (shoppingCartRecord!=null){
            shoppingCartRecord.setUserId(BaseContext.getCurrentId());
            shoppingCartRecord.setNumber(shoppingCartRecord.getNumber()+1);
//            shoppingCartRecord.setAmount(shoppingCartRecord.getAmount().add(danJia));
            shoppingCartService.updateById(shoppingCartRecord);
            return R.success(shoppingCartRecord);
        }
//        D. 如果不存在，则添加到购物车，数量默认就是1
        else if(shoppingCartRecord==null){
            Long currentId = BaseContext.getCurrentId();
            shoppingCart.setUserId(currentId);
            shoppingCartService.save(shoppingCart);
        }
        return R.success(shoppingCart);
    }

    /**
     * 根据用户id查询购物车信息
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> queryShoppingCart(){
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return R.success(list);
    }

    /**
     * 取消购物车（-1）
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart){  //setmealId: "1535797944991723521"
/*//        当前分量
        BigDecimal dishNub = new BigDecimal(shoppingCart.getNumber());
//        当前单价
        BigDecimal danJia = shoppingCart.getAmount().divide(dishNub);*/
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId, shoppingCart.getDishId());
        ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
//        设置数量
        Integer number = one.getNumber();
        if ((number = number-1)>=0){
            one.setNumber(number);
            shoppingCartService.updateById(one);
        }else {
//            删除该条数据
            shoppingCartService.removeById(shoppingCart.getId());
            return R.success(null);
        }
//        设置金额
/*        one.setAmount(one.getAmount().subtract(danJia));*/
        return R.success(one);
    }

    /**
     * 清空购物车 √
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> cleanShoppingCart(){

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        return R.success("购物车清空成功");
    }
}
